const handel = require("./handel");
let fun = {
    cnf:async(config,data,Constraints)=>{
        if(config.paging){
            if(!data.pageSize){
                throw new Error("缺少pageSize参数");
            }else{
                if(!/^[+]{0,1}(\d+)$/.test(data.pageSize)){
                    throw new Error("pageSize参数错误");
                }
            }
            if(!data.currentPage){
                throw new Error("缺少currentPage参数");
            }else{
                if(!/^[+]{0,1}(\d+)$/.test(data.currentPage)){
                    throw new Error("currentPage参数错误");
                }
            }
        }

        let sql = config.sql(data);
        let parameter = sql.match(/{([^}]*)}/g)||[]
        sql = sql.replace(/{([^}]*)}/g, "?")
        parameter.forEach((r,index)=>{
            parameter[index]=r.replace(/{/g, "").replace(/}/g, "")
        })
        let cnf = fun.getData(data,parameter,Constraints,sql)
        if(config.paging){
            let tableCountSql =`select count(1) count from (${cnf.sql})tableA`
            let count = await handel.query(tableCountSql,cnf.data);
            let startData = (data.currentPage-1)*data.pageSize;
            let endData = startData+Number(data.pageSize) ;
            cnf.sql = `select * from (${cnf.sql})tableA limit  ? , ? `;
            cnf.data.push(startData);
            cnf.data.push(endData);
            return {
                count:count[0].count,
                pageSize:data.pageSize,
                currentPage:data.currentPage,
                startData,
                endData,
                tableList:await handel.query(cnf.sql,cnf.data)
            }
        }else{
            return await handel.query(cnf.sql,cnf.data)
        }
    },
    getData:(data,parameter,Constraints,sql)=>{
        let f_data = [];
        parameter.forEach((text)=>{
            let constraint = getConstraint(text,Constraints);
            data[text]=data[text]||""
            //去空格
            if(constraint.isTrim){
                data[text] = data[text].replace(/\s/g,"");
            }
            //判断空
            if(!constraint.isNull){
                if(!data[text]){
                    throw new Error("参数错误");
                }
            }
            //判断方法过滤器
            if(constraint.fun){
                let fun = constraint.fun(data[text]);
                if(!fun.type){
                    throw new Error(fun.msg);
                }
            }
            //判断正则过滤器
            if(constraint.expression){
                if (!constraint.expression.text.test(data[text])) {
                    throw new Error(constraint.expression.msg);
                }
            }
            f_data.push(data[text])
        })
        return{sql,data:f_data} ;
    }
}
function getConstraint(text,Constraints){
    let constraint = {};
    Constraints.forEach((obj)=>{
        if(obj.name===text){
            constraint = obj;
        }
    })
    return constraint;
}
module.exports = fun;